package com.f6.weixin.services;

import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.f6.weixin.common.vo.TextMessage;
import com.f6.weixin.utils.MessageUtil;
import com.f6.weixin.utils.WeiXinUtils;

@Service
public class MessageService {
	private Logger logger = LoggerFactory.getLogger(MessageService.class);

	public TextMessage processMessage(Map<String, String> requestMap) {
		Set<String> keyset = requestMap.keySet();

		for (Iterator<String> it = keyset.iterator(); it.hasNext();) {
			String key = it.next();
			String value = requestMap.get(key);
			logger.info(key + "----" + value);
		}
		String respMessage = null;
		TextMessage textMessage = null;
		try {
			// 默认返回的文本消息内容
			String respContent = WeiXinUtils.utf8str("请求处理异常，请稍候尝试！");
			// 发送方帐号（open_id）
			String fromUserName = requestMap.get("FromUserName");
			// 公众帐号
			String toUserName = requestMap.get("ToUserName");
			// 消息类型
			String msgType = requestMap.get("MsgType");

			// 回复文本消息
			textMessage = new TextMessage();
			textMessage.setToUserName(toUserName);
			textMessage.setFromUserName(fromUserName);
			textMessage.setCreateTime(new Date().getTime());
			textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
			textMessage.setFuncFlag(0);
			textMessage.setMsgType(msgType);
			String eventkey = requestMap.get("EventKey");
			textMessage.setEventKey(eventkey);

			// 文本消息
			if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) {
				respContent = WeiXinUtils.utf8str("您发送的是文本消息！");
			}
			// 图片消息
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_IMAGE)) {
				respContent = WeiXinUtils.utf8str("您发送的是图片消息！");
			}
			// 地理位置消息
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LOCATION)) {
				respContent = WeiXinUtils.utf8str("您发送的是地理位置消息！");
			}
			// 链接消息
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LINK)) {
				respContent = WeiXinUtils.utf8str("您发送的是链接消息！");
			}
			// 音频消息
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_VOICE)) {
				respContent = WeiXinUtils.utf8str("您发送的是音频消息！");
			}
			// 事件推送
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)) {
				// 事件类型
				String eventType = requestMap.get("Event");
				textMessage.setEventType(eventType);
				// 订阅
				if (eventType.equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)) {
					respContent = WeiXinUtils.utf8str("谢谢您的关注！");
				}
				// 取消订阅
				else if (eventType.equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)) {
					 
					respContent = WeiXinUtils.utf8str("you are unsubscribe:" + eventkey);
				}
				// 自定义菜单点击事件
				else if (eventType.equals(MessageUtil.EVENT_TYPE_CLICK)) {
					 
					respContent = WeiXinUtils.utf8str("you are clicking EventKey:" + eventkey);
				}
				// 自定义菜单点击事件
				else if (eventType.equals(MessageUtil.EVENT_TYPE_SCAN)) {
					respContent = WeiXinUtils.utf8str("you are scan the qrcode =>:" + eventkey);
				}
			}
			logger.info("@@@@@@@@@@@@@@@@" + respContent);
			textMessage.setContent(respContent);
			respMessage = MessageUtil.textMessageToXml(textMessage);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return textMessage;
	}

}
